package com.threeonemeeting.adapter;

import java.net.URLEncoder;
import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.threeonemeeting.Const;
import com.threeonemeeting.R;
import com.threeonemeeting.util.MiscUtil;


public class MeetingDataImageListAdapter extends ArrayAdapter<String> {

	private ArrayList<String> mImagesItems  = null;
	
	public void setImageList(ArrayList<String> imageList) {
		mImagesItems = imageList;
		notifyDataSetChanged();
	}

	public ArrayList<String> getImageList() {
		return mImagesItems;
	}

	@Override
	public void add(String imageUrl) {
		if (mImagesItems == null)
			mImagesItems = new ArrayList<String>();

		mImagesItems.add(imageUrl);
	}

	public void clear() {
		if (mImagesItems != null) {
			mImagesItems.clear();
		}
	}

	@Override
	public int getCount() {
		if (mImagesItems == null)
			return 0;

		return mImagesItems.size();
	}

	@Override
	public String getItem(int position) {
		return mImagesItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public MeetingDataImageListAdapter(Activity activity) {
		super(activity, 0);
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		Activity activity = (Activity) getContext();

		String strImageUrl = getItem(position);

		View rowView = convertView;
		MeetingDataImageCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.gvi_meeting_data_image, null);
			viewCache = new MeetingDataImageCache(rowView);
			rowView.setTag(viewCache);
		} else {
			viewCache = (MeetingDataImageCache) rowView.getTag();
		}

		// 图片
		ImageView imageView = viewCache.getDataImageView();
		String strThumbUrl = Const.URL.BASE_THUMB_URL;
		strThumbUrl += URLEncoder.encode(strImageUrl);
		MiscUtil.setImageView(imageView, strThumbUrl);

		return rowView;
	}

	public class MeetingDataImageCache {

		private View baseView;
		private ImageView meetingDataFileImageView;

		public MeetingDataImageCache(View baseView) {
			this.baseView = baseView;
		}

		public ImageView getDataImageView() {
			if (meetingDataFileImageView == null) {
				meetingDataFileImageView = (ImageView) baseView
						.findViewById(R.id.meeting_data_image);
			}
			return meetingDataFileImageView;
		}
	}

}
